package pacman.state;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import pacman.base.Score;
import pacman.util.Clock;

public class Pause extends BasicGameState {

    public static final int ID = 2;
    private StateBasedGame sbg;
    private GameContainer gc;
    private AngelCodeFont font;
    private String[] options = new String[] { "Continue", "Quit" };
    private Image logo;
    private int selected;
    private FadeOutTransition fot;
    private FadeInTransition fit;

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		this.sbg = sbg;
		this.gc = gc;
		fot = new FadeOutTransition(Color.black);
		fit = new FadeInTransition(Color.black);
		try {
		    font = new AngelCodeFont("data/demo2.fnt", "data/demo2_00.tga");
		    logo = new Image("data/logo.gif");
		 } catch (Exception e) {
			 System.out.println(e.getMessage());
		 }
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.setFont(font);
		g.drawImage(logo, 320, 20);
		g.drawString("Score :" + Score.getScore(), 200, 80);
		g.drawString("Time : " + Clock.getTime(), 200, 120);
		for (int i = 0; i < options.length; i++) {
		    g.drawString(options[i], 400 - (font.getWidth(options[i]) / 2), 200 + (i * 50));
		    if (selected == i) g.drawRect(200, 190 + (i * 50), 400, 50);
		}
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    }

    public void keyReleased(int key, char c) {
		if (key == Input.KEY_DOWN) {
		    selected++;
		    if (selected >= options.length) selected = 0;	    
		}
		if (key == Input.KEY_UP) {
		    selected--;
		    if (selected < 0) selected = options.length - 1;
		}
		if (key == Input.KEY_ENTER) {
		    if (selected == 0) sbg.enterState(Game.ID, fot, fit);
		    if (selected == 1) sbg.getContainer().exit();
		}
		if (key == Input.KEY_F2) {
		    try {
				if (!gc.isFullscreen()) gc.setFullscreen(true); 
				else gc.setFullscreen(false);
		    } catch (Exception e) {
		    	System.out.println(e.getMessage());
		    }
		}
    }
    @Override
    public int getID() {
    	return ID;
    }
}
